from PySide6.QtWidgets import QWidget, QSizePolicy

from gui.reference import DX, LINE_LENGTH, xy_to_rc


class BaseWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('打造超级战士模拟器')
        self.setGeometry(300, 300, 1000, 618)
        self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
        self.setFixedSize(self.size())

    def keyPressEvent(self, event):
        if hasattr(self, 'player'):
            key = event.key()
            move_status = getattr(self, 'player').move(key)

            if move_status:
                self.update()

    def mouseReleaseEvent(self, event):
        if hasattr(self, 'player'):
            pos = event.pos()
            px, py = pos.x(), pos.y()

            if DX < px < DX + LINE_LENGTH and DX < py < DX + LINE_LENGTH:
                row, col = xy_to_rc((px, py))

                move_status = getattr(self, 'player').move_by_pos((row, col))
                if move_status:
                    self.update()
